<template>
  <div>
    <div class="box">
      <div id="preview" v-on:paste="getClipboardFiles">
        <span>将图片按Ctrl+V 粘贴至此处</span>
        <img id="showImg" v-bind:src="imgSrc" alt="">
      </div>
      <button v-on:click="changeDisplay">按行\段显示</button>
      <button class="copyBtn" v-on:click="onCopy">复制</button>
      <button v-on:click="onClear">清空</button>
      <div class="result">
        <p v-bind:class="showType" v-for="res in result">{{ res.words }}</p>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "upload",
  data() {
    return {
      imgSrc: '',
      access_token: '',
      result: [],
      form: {},
      showType: 'line',
    }
  },
  created() {

    const API_KEY = 'PAQn1HXU8rCXkrWEEmVHgb3B'
    const SECRET_KEY = '9HYiZEifRzFNKyQT6LvbmpIGLYH6p3si'
    this.axios.get(
        `/baiduApi/oauth/2.0/token?grant_type=client_credentials&client_id=${API_KEY}&client_secret=${SECRET_KEY}&`,
        {
          headers: {
            dataType: 'json',
            'Access-Control-Allow-Origin': '*',
          }
        }).then(res => {
      console.log(res)
      if (res.status === 200) {
        this.access_token = res.data.access_token;
      }
    })
  },
  methods: {
    onCopy(){

    },
    // 监听粘贴操作
    getClipboardFiles(event) {
      let items = event.clipboardData && event.clipboardData.items;
      let file = null
      if (items && items.length) {
        // 检索剪切板items
        for (var i = 0; i < items.length; i++) {
          if (items[i].type.indexOf('image') !== -1) {
            file = items[i].getAsFile()
          }
        }
      }
      if (!file) {
        this.$message.error('粘贴内容不是图片')
        return;
      }
      this.handleFilesAdd(file)
    },

    handleFilesAdd(file) {
      this.getImg(file)
    },

    getImg(file) {
      let imageBase = "";
      let reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = (e) => {
        imageBase = e.target.result.replace("data:image/png;base64,", "");
        this.imgSrc = "data:image/png;base64," + imageBase
        let form = new FormData();
        form.append("image", imageBase);
        form.append("access_token", this.access_token);
        this.uploadPlans(form)
      }
    },

    uploadPlans(form) {
      this.axios.post(
          '/baiduApi/rest/2.0/ocr/v1/general_basic',
          form,
          {
            headers: {
              dataType: 'json',
              contentType: 'application/x-www-form-urlencoded',
            }
          }).then(res => {
        if (res.status === 200) {
          console.info(res);
          let data = res.data.words_result;
          this.result = data
          console.log(data)
        }
      })
    },
    onClear(){
      this.result = []
      this.imgSrc =''
      this.form = {}
    },
    changeDisplay(){
      if(this.showType === 'line'){
        this.showType = 'block'
      }else{
        this.showType = 'line'
      }
    }
  }
}
</script>

<style scoped>
* {
  padding: 0;
  margin: 0;
}

.box {
  display: flex;
  flex-direction: column;
  align-items: center;
}


#preview, .result {
  width: 80%;
  min-height: 300px;
  background-color: gray;
  border: 1px dotted black;
}

#preview img {
  width: 100%;
}

.result > p {
  text-align: left;
}

.block{
  display: inline;
  text-align: left;
}

.line{
  display: block;
  text-align: left;
}
</style>